
import scala.io.Source

final class IteratorExtra[T](val self: Iterator[T]) {
  def count(op: T => Boolean): Int = 
    self.foldLeft(0) { (r, x) => if (op(x)) r + 1 else r }
}

implicit def iter2IterExtra[T](x: Iterator[T]): IteratorExtra[T] =
  new IteratorExtra(x)

object FileExtra {
  def apply(s: String) = new FileExtra(s)
}

class FileExtra(s: String) {
  def chars = getSource
  def lineCount = chars.count(_ == '\n')
  def lines = getSource.getLines
  def foreach(op: String => Unit) = lines.foreach(op)
  private def getSource = Source.fromFile(s)
}

if (args.length > 0) {
  val path = args(0)

  val maxLength = FileExtra(path).lineCount.toString.length
  val fmtString = "%%%dd| %%s".format(maxLength)

  var lineNo = 1
  FileExtra(path).foreach { line =>
    print(fmtString.format(lineNo, line))
    lineNo += 1
  }
}

// おしまい
